package dataMapper;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import dataEntity.Weather;


public class WeatherFileMapper {
	public static  Map<String,Map<String,Weather>> process(BufferedReader br){
		Map<String,Map<String,Weather>> map = new HashMap<String,Map<String,Weather>> ();
		try {
			br.readLine();
			while(br.ready()){
				Weather object = buildObject(br.readLine());

				Map<String,Weather> stateMap = map.get(object.getState());
				if(stateMap == null) stateMap = new HashMap<String,Weather>();
				stateMap.put(object.getCounty(), object);
				map.put(object.getState(), stateMap);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return map;
	}
	
	public static Weather buildObject(String inLine){
		String[] input = inLine.split("\t");
		String[] inches = input[1].split(" ");
		String[] county = input[2].split("/");
		String[] state = county[0].split(",");
		String population = county[1].trim().replace(",", "");
		Weather object = new Weather();
		object.setRank(input[0].replace(".", ""));
		object.setInches(inches[0]);
		object.setCounty(state[0]);
		object.setState(state[1].trim());
		object.setPopulation(population);
		return object;
	}
}
